<template>
  <el-container class="custom-locker">
    <el-main>
      <el-form @submit.prevent>
        <el-input v-model="password" type="password" placeholder="请输入密码解锁" @keyup.enter="handleUnlock" />
      </el-form>
      <el-link type="default" @click="handleLogout">退出登陆</el-link>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import { useRouter } from 'vue-router'

const password = ref('')
const userStore = useUserStore()
const router = useRouter()


const handleUnlock = () => {
  const decryptUser = userStore.decryptUser

  if (!decryptUser.lockpassword) {
    router.go(-1)
    return
  }

  if (password.value === decryptUser.lockpassword) {
    password.value = ''
    delete decryptUser.lockpassword
    userStore.setEncryptUser(decryptUser)
    console.log(router.currentRoute.value.query.redirect)
    router.push(router.currentRoute.value.query.redirect || '/home/workspace')
  } else {
    password.value = ''
    ElMessage.error('锁屏密码输入错误，请重新输入')
  }
}

const handleLogout = () => {
  localStorage.removeItem('isAuthenticated')
  userStore.clearUserInfo()
  router.push('/login')
}
</script>

<style>
.custom-locker {
  background-image: url("@/assets/imgs/lock-bg.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 100%;
}
</style>
